package com.zhenyi.websocket.core.ws;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

public abstract class WebSocketChannelInitializer2 extends ChannelInitializer<SocketChannel> {

	// private final SimpleChannelInboundHandler<TextWebSocketFrame> handler;
	private String path;

	public void setPath(String path) {
		this.path = path;
	}

	public abstract SimpleChannelInboundHandler<TextWebSocketFrame> onAddHandler();

	@Override
	protected void initChannel(SocketChannel ch) throws Exception {
		ChannelPipeline pipeline = ch.pipeline();
		// HttpServerCodec: 针对http协议进行编解码
		pipeline.addLast("httpServerCodec", new HttpServerCodec());
		// ChunkedWriteHandler分块写处理，文件过大会将内存撑爆
		pipeline.addLast("chunkedWriteHandler", new ChunkedWriteHandler());
		/**
		 * 作用是将一个Http的消息组装成一个完成的HttpRequest或者HttpResponse，那么具体的是什么 取决于是请求还是响应,
		 * 该Handler必须放在HttpServerCodec后的后面
		 */
		pipeline.addLast("httpObjectAggregator", new HttpObjectAggregator(8192));

		// 用于处理websocket, /ws为访问websocket时的uri
		pipeline.addLast("webSocketServerProtocolHandler", new WebSocketServerProtocolHandler(path));
		pipeline.addLast("myWebSocketHandler", onAddHandler());
		// pipeline.addLast("myWebSocketHandler", new
		// SimpleChannelInboundHandler<TextWebSocketFrame>() {
		//
		// @Override
		// protected void channelRead0(ChannelHandlerContext ctx,
		// TextWebSocketFrame msg) throws Exception {
		//
		// String text = msg.text();
		// System.out.println(text);
		// // Ret ret = Ret.by("status", 200);
		// // ctx.channel().writeAndFlush(new
		// // TextWebSocketFrame(ret.toJson()));
		// // final FullHttpRequest request = (FullHttpRequest) msg;
		// // final HttpMethod method = request.method();
		// // final String uri = request.uri();
		// // System.out.println(uri);
		// }
		//
		// @Override
		// public void handlerAdded(ChannelHandlerContext ctx) throws Exception
		// {
		//
		// System.out.println("handlerAdded " + ctx.name());
		// super.handlerAdded(ctx);
		// ChannelGroups.add(ctx.channel());
		// Ret ret = Ret.by("status", 201);
		// ret.set("size", ChannelGroups.size());
		// ret.set("dir", "add");
		// ChannelGroups.broadcast(new TextWebSocketFrame(ret.toJson()));
		// }
		//
		// @Override
		// public void handlerRemoved(ChannelHandlerContext ctx) throws
		// Exception {
		// System.out.println("handlerRemoved");
		// super.handlerRemoved(ctx);
		// ChannelGroups.discard(ctx.channel());
		// Ret ret = Ret.by("status", 201);
		// ret.set("size", ChannelGroups.size());
		// ret.set("dir", "remove");
		// ChannelGroups.broadcast(new TextWebSocketFrame(ret.toJson()));
		// }
		// });
		ch.config().setKeepAlive(true);
		ch.config().setTcpNoDelay(true);
	}

}
